home *** CD-ROM | disk | FTP | other *** search
- Path: news.sinet.slb.com!usenet
- From: "Vinh D. Nguyen" <vnguyen@sugar-land.anadrill.slb.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Memory Leak?
- Date: Fri, 15 Mar 1996 08:45:04 -0600
- Organization: Schlumberger Anadrill
- Message-ID: <31498270.7661@sugar-land.anadrill.slb.com>
- References: <4i2tr9$q6k@newsgate.dircon.co.uk>
- NNTP-Posting-Host: 163.185.118.40
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- Chris Pyman wrote:
- >
- > main(int argc, char* argv[]) {
- > CString s1("31-12-99");
- > CString s2;
- >
- > s2 = s1.Mid(0,2) + s1.Mid(3,2) + s1.Mid(6,2);
- >
- > cout << s2;
- > return 0;
- > }
- >
- > Obviously the idea is to set s2 to "311299", but I would have thought
- > that the Mid() method creates a new CString object and returns a
- > reference to it, in which case what becomes of the three "temporary"
- > objects used to build the string in s2? Are they just floating around
- > with no way to get at them, or do they get properly deleted? And if
- > the latter, how does MFC manage it?
- >
-
- No, you will not experience any memory leakage as long as your CString constructor
- properly deallocates the buffer used to hold the characters. You are correct that
- the CString::Mid method returns temporary strings but these temporary strings
- are created on the stack (the buffers holding the actual characters are
- created on the heap of course) and will be destroyed properly by the time the
- function exits. I am not sure when these temporary objects actually get destroyed
- but I imagine it would depend on the compiler. A smart compiler should recognize
- these objects as temporary and delete them as soon as the statement has been
- executed. (There is no need to keep something around that the programmer has no way
- to get to!) A dumb compiler would probably keep them around until the function
- exits. Can anyone out there enlighten me on this?
-
- --
- --------------------------------------------------------------------------
- * Vinh Nguyen vnguyen@slb.com *
- * Drilling Information Products - Senior Engineer *
- * Anadrill Schlumberger *
- * 200 Gillingham Ln. (713) 275-7524 (Office) *
- * Sugarland, TX 77478 (713) 275-8098 (FAX) *
- --------------------------------------------------------------------------
-